package com.xayy.health.core.utils;

import cn.hutool.core.util.StrUtil;
import com.xayy.health.core.constant.HealthManagerConstant;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalTimeUtil
 *
 * @author minco
 * @since 2024-03-13 17
 */
public class LocalTimeYYUtil {
    private static final String datetime1 = "yyyy-MM-dd HH:mm:ss";
    private static final String datetime2 = "yyyy-MM-dd HH:mm:ss.SS";
    private static final String datetime3 = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final String datetime4 = "yyyy-MM-dd HH:mm:ss.S";


    private static DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(datetime1);
    private static DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(datetime2);
    private static DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern(datetime3);
    private static DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern(datetime4);


    /**
     * 支持毫秒
     *
     * @param datetimeString
     * @return
     */
    public static LocalDateTime parse(String datetimeString) {
        if (StrUtil.isBlank(datetimeString)) {
            return null;
        }
        int da = datetimeString.length();
        DateTimeFormatter dateTimeFormatter = null;
        if (da == datetime1.length()) {
            dateTimeFormatter = formatter1;
        } else if (da == datetime2.length()) {
            dateTimeFormatter = formatter2;
        } else if (da == datetime3.length()) {
            dateTimeFormatter = formatter3;
        } else {
            dateTimeFormatter = formatter4;
        }
        return LocalDateTime.parse(datetimeString, dateTimeFormatter);
    }
}
